//
//  Currently set up as a linear line between two factions,
//  but can be redesigned to incorporate multiple factions up to infinity.
//

class BG3Objective extends Actor
    placeable;

var() StaticMeshComponent StaticMesh;
var LightEnvironmentComponent LightEnvironment;


var() float ConvRadius;
var() float ConvPointsPerPawn;
var int numberOfCloseAllyPawns;
var int numberOfCloseEnemyPawns;
var() string Team;


//percent to being converged; starts at 50 when neutral then 100 for allies; 0 for axis;
var() float CurrConvAmount;


//set up to override if needed later
simulated function PostBeginPlay()
{
    super.PostBeginPlay();

    if(Team == "neutral")
    {
        CurrConvAmount = 50;
    }
    else
    {
        CurrConvAmount = 100;
    }

    SetTimer(5.0, true);
}

function Timer()
{
    local float amtToConverge;

    //get close pawns
    UpdateClosePawns();

    if(numberOfCloseAllyPawns > 0 && numberOfCloseEnemyPawns > 0)
    {
        //do nothing
        amtToConverge = 0;
    }
    else if(numberOfCloseAllyPawns > 0)
    {
        //if less than 100% be sure to add back to it;
        amtToConverge = numberOfCloseAllyPawns * ConvPointsPerPawn;
    }
    else if(numberOfCloseEnemyPawns > 0)
    {
        amtToConverge = numberOfCloseEnemyPawns * ConvPointsPerPawn;
    }

    AddConversionAmount(amtToConverge);

}

function AddConversionAmount(float amt)
{
    CurrConvAmount += amt;

    //handle special scoring cases based on neutrality
    if(Team == "neutral")
    {
        //Convert to ally
        if(CurrConvAmount >= 100)
        {
            Team ="allies";
            CurrConvAmount = 100;
        }
        //axis
        else
        {
            team = "axis";
            CurrConvAmount = 100;
        }
    }
    else
    {
        //keep current faction
        if(CurrConvAmount >= 100)
        {
            Team ="allies";
            CurrConvAmount = 100;
        }
        //convert to the other team
        else if(CurrConvAmount <= 0)
        {
            if(team == "allies")
            {
                team = "axis";
                CurrConvAmount = 100;
            }
            //team must be axis here
            else
            {
                team = "allies";
                CurrConvAmount = 100;
            }
        }
    }
}


//done from timer to conserve CPU usage
function UpdateClosePawns()
{
    //touching actors would only get Pawns extra close to us,
    //so do it for all pawns and see if VSize < ConvRadius
    //also make sure PawnTeam = OurTeam

    local BG3Pawn p;

    foreach DynamicActors(class'BG3Pawn', p)
    {
        //make sure it is in radius
        if(VSize(location - p.location) <= ConvRadius)
        {
            if(p.Team == Team)
            {

                numberOfCloseAllyPawns++;
            }
            else
            {
                numberOfCloseEnemyPawns++;
            }

            //just want to let the pawn know that
            //they're in our radius for HUD reasons later

            p.NotifyInObjectiveRadius(self);
        }

    }

}

function bool isPawnInOurRadius(BG3Pawn p)
{
    return (VSize(p.location - location) <= ConvRadius);
}

DefaultProperties
{
    Begin Object Class=DynamicLightEnvironmentComponent Name=MyLightEnvironment
	   bEnabled=TRUE
    End Object
    LightEnvironment=MyLightEnvironment
    Components.Add(MyLightEnvironment)
    begin object class=StaticMeshComponent Name=BaseMesh
    	StaticMesh=StaticMesh'EngineMeshes.Cube'
    	LightEnvironment=MyLightEnvironment
    end object
    Components.Add(BaseMesh)


    StaticMesh=BaseMesh
    CollisionComponent=BaseMesh
    bCollideActors=true
    bBlockActors=true

    
    //our defined fields
    ConvRadius=512;
    ConvPointsPerPawn=20; // 20 for testing, realistically 1, 2, or 3 in a real game
    Team="neutral"

}